import React, { Component } from 'react';
import { Modal, message, Col, Row, Tag, Radio, Form, Card, Tree } from 'antd';

const { TreeNode } = Tree;
const FormItem = Form.Item;
const RadioGroup = Radio.Group;
export default class SetWIFI extends Component {
  state = {
    devIdArray: this.props.selectedRows,
  };
  onCheck = checkedKeys => {
    //console.log('selectkeys', checkedKeys);
    this.setState({ devIdArray: checkedKeys });
  };
  handleSet = () => {
    const { dispatch } = this.props;
    const devIds = this.state.devIdArray.join(',');
    this.props.form.validateFields((err, values) => {
      const { wuSwitch } = values;
      if (!err) {
        dispatch({
          type: 'dev/setWIFI',
          payload: { devIds, wuSwitch },
          callback: () => {
            message.success('设置WIFI/USB成功.');
          },
        });
        this.props.handleModalVisible(false);
      }
    });
  };

  render() {
    const { selectedRows } = this.props;
    const { modalVisible, handleModalVisible, form } = this.props;
    return (
      <Modal
        title="设置WIFI/USB"
        visible={modalVisible}
        onOk={this.handleSet}
        onCancel={() => handleModalVisible(false)}
      >
        <Form>
          <FormItem labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} label="WIFI/USB开关: ">
            {form.getFieldDecorator('wuSwitch', {
              initialValue: 1,
              rules: [{ required: true, message: '请选择WIFI/USB开关...' }],
            })(
              <RadioGroup>
                <Radio value={0}>关</Radio>
                <Radio value={1}>开</Radio>
              </RadioGroup>
            )}
          </FormItem>
        </Form>
        <Row style={{ width: '100%' }}>
          <Card title="设备列表（多选）" bodyStyle={{ height: 600, overflow: 'scroll' }}>
            {selectedRows.length ? (
              <Tree
                checkable
                style={{ height: 600 }}
                defaultCheckedKeys={selectedRows}
                onCheck={this.onCheck}
              >
                {selectedRows.map(data => (
                  <TreeNode title={data} key={data} />
                ))}
              </Tree>
            ) : (
              'loading tree'
            )}
          </Card>
        </Row>
      </Modal>
    );
  }
}
